﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using AutoMapper;
using Jarvis.Server.Location;
using Jarvis.Server.Statistics;
using Jarvis.WCF.DataContracts;
using Jarvis.WCF.ServiceContracts;

namespace Jarvis.Server
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class JarvisWcfService: ILocatorService,IStatisticsService
    {
        private readonly ILocationProvider _locationProvider;
        private IMappingEngine _mappingEngine;
        private IStatisticsCollector _statisticsCollector;

        public JarvisWcfService(ILocationProvider locationProvider, IMappingEngine mappingEngine, IStatisticsCollector statisticsCollector)
        {
            _locationProvider = locationProvider;
            _mappingEngine = mappingEngine;
            _statisticsCollector = statisticsCollector;

            _locationProvider.CurrentLocationChanged += CurrentLocationChanged;
        }

        void CurrentLocationChanged(object sender, LocationEventArgs e)
        {
            OperationContext.Current.GetCallbackChannel<ILocatorServiceCallback>().OnCurrentLocationChanged(_mappingEngine.Map<WCF.DataContracts.Location>(e.Location));
        }

        #region Implementation of ILocatorService

        public IList<WCF.DataContracts.Location> GetKnownLocations()
        {
            return _locationProvider.KnownLocations
                .Select<Domain.Location, WCF.DataContracts.Location>(
                    l =>
                    {
                        var mapped = _mappingEngine.Map<WCF.DataContracts.Location>(l);
                        mapped.Score =
                            l.LocationSensorDatas.DistanceFrom(_locationProvider.CurrentLocation.LocationSensorDatas);
                        return mapped;
                    }).ToList();
        }

        public WCF.DataContracts.Location GetCurrentLocation()
        {
            return _mappingEngine.Map<WCF.DataContracts.Location>(_locationProvider.ComputedLocation);
        }

        public void StoreLocation(WCF.DataContracts.Location location)
        {
            var l = _mappingEngine.Map<Domain.Location>(location);

            _locationProvider.StoreAsKnownLocation(l);
        }

        public void Update(WCF.DataContracts.Location location)
        {
            var l = _mappingEngine.Map<Domain.Location>(location);

            _locationProvider.UpdateLocation(l);
        }

        public void Remove(WCF.DataContracts.Location location)
        {
            var l = _mappingEngine.Map<Domain.Location>(location);

            _locationProvider.RemoveLocation(l);
        }

        #endregion

        #region Implementation of IStatisticsService

        public LocationStats GetStatistics()
        {
            throw new NotImplementedException();
        }

        public LocationStatsEntry GetStatisticsForLocation(WCF.DataContracts.Location location)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
